package com.rememberme;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;

import com.business.ReminderBus;
import com.dataobject.Group;

public class RemindList extends Activity implements OnClickListener {
	private SparseArray<Group> _Groups = new SparseArray<Group>();
	private ReminderBus _Bus = new ReminderBus(this);
	private ExpandableListView exListView;
	private MyExpandableListAdapter adapter;
	private LinearLayout mainView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.remind_list);
		createData();
		exListView = (ExpandableListView) findViewById(R.id.listView);
		adapter = new MyExpandableListAdapter(this, _Groups, _Bus.get_MyDB());
		exListView.setAdapter(adapter);
		mainView = (LinearLayout) findViewById(R.id.mainView);
		mainView.setOnClickListener(this);
	}

	@Override
	protected void onRestart() {
		super.onRestart();
		createData();
		MyExpandableListAdapter adapter = new MyExpandableListAdapter(this,
				_Groups, _Bus.get_MyDB());
		adapter.notifyDataSetChanged();
		exListView.setAdapter(adapter);
	}

	private void createData() {
		Group[] group = new Group[4];
		group[0] = new Group();
		group[1] = new Group();
		group[2] = new Group();
		group[3] = new Group();
		group[0].set_SubItems(_Bus.getPreviousList());
		group[0].set_DatePeriod("Cũ ");
		group[0].set_ViewSize("(" + group[0].get_SubItems().size() + ")");
		group[1].set_SubItems(_Bus.getTodayList());
		group[1].set_DatePeriod("Hôm nay ");
		group[1].set_ViewSize("(" + group[1].get_SubItems().size() + ")");
		group[2].set_SubItems(_Bus.getTomorrowList());
		group[2].set_DatePeriod("Ngày mai ");
		group[2].set_ViewSize("(" + group[2].get_SubItems().size() + ")");
		group[3].set_SubItems(_Bus.getNextList());
		group[3].set_DatePeriod("Ngày tới ");
		group[3].set_ViewSize("(" + group[3].get_SubItems().size() + ")");
		_Groups.append(0, group[0]);
		_Groups.append(1, group[1]);
		_Groups.append(2, group[2]);
		_Groups.append(3, group[3]);
	}

	@Override
	public void onClick(View v) {
		if (v.equals(mainView)) {
			Dialog dialog = initContextMenu();
			dialog.show();
		}
	}

	public Dialog initContextMenu() {
		AlertDialog.Builder builder = new Builder(this);
		final String[] context_arr = new String[] {
				getResources().getString(R.string.context_add),
				getResources().getString(R.string.context_deleteAll) };
		builder.setTitle(getResources().getString(R.string.context_select))
				.setItems(context_arr, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int pos) {
						dialog.dismiss();
						switch (pos) {
						// ---------- ---------- ----------
						// ADD
						// ---------- ---------- ----------
						case 0: {
							Intent intent = new Intent(getApplicationContext(),
									RemindEditor.class);
							intent.putExtra("isNew", true);
							startActivity(intent);
							break;
						}
						// ---------- ---------- ----------
						// DELETE ALL
						// ---------- ---------- ----------
						case 1: {
							_Bus.deleteAll();
							for (int i = 0; i < _Groups.size(); i++) {
								_Groups.get(i).get_SubItems().clear();
								_Groups.get(i).set_ViewSize("(0)");
								adapter.notifyDataSetChanged();
								exListView.setAdapter(adapter);
							}
							break;
						}
						}
					}
				});
		return builder.create();
	}
}
